package com.itbaizhan.paymentdemo.service.impl;

import com.alibaba.fastjson.JSON;
import com.itbaizhan.paymentdemo.entity.PaymentInfo;
import com.itbaizhan.paymentdemo.enums.PayType;
import com.itbaizhan.paymentdemo.mapper.PaymentInfoMapper;
import com.itbaizhan.paymentdemo.service.IPaymentInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itbaizhan
 * @since 04-26
 */
@Slf4j
@Service
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo> implements IPaymentInfoService {

    /*添加交易记录*/
    @Override
    public void createPaymentInfo(Map<String, Object> plainTextMap) {

        log.info("**********记录交易日志********");

        // 订单编号
        String orderNo = (String)plainTextMap.get("out_trade_no");
        // 微信支付订单号
        String transactionId = (String)plainTextMap.get("transaction_id");
        // 交易类型
        String tradeType = (String)plainTextMap.get("trade_type");
        // 交易状态
        String tradeState = (String)plainTextMap.get("trade_state");
        Map<String, Object> amount = (Map)plainTextMap.get("amount");
        //用户支付金额
        Integer payerTotal = ((Double) amount.get("payer_total")).intValue();
        PaymentInfo paymentInfo = new PaymentInfo();
        // 订单编号
        paymentInfo.setOrderNo(orderNo);
        // 交易类型
        paymentInfo.setPaymentType(PayType.WXPAY.getType());
        // 微信支付订单号
        paymentInfo.setTransactionId(transactionId);
        // 交易类型
        paymentInfo.setTradeType(tradeType);
        // tradeState
        paymentInfo.setTradeState(tradeState);
        // 用户支付金额
        paymentInfo.setPayerTotal(payerTotal);
        // 通知参数   map转为字符串
        paymentInfo.setContent(JSON.toJSONString(plainTextMap));
        paymentInfo.setCreateTime(LocalDateTime.now());
        baseMapper.insert(paymentInfo);

    }
}
